package gui;

import jade.core.LifeCycle;

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import observer.Broker;
import observer.Observer;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JToggleButton;
import javax.swing.JSeparator;
import javax.swing.JFormattedTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerNumberModel;

import utility.Auto;
import utility.Triple;
import javax.swing.JList;

public class GeneralPanel extends JPanel implements ActionListener,Observer{

	private JFileChooser chooser;

	private Broker broker;

	private JButton startButton,stopButton,impostaMappaButton,buttonAddAuto,buttonAddConfAuto
	,btnClearSelectionSigleAuto,btnPause,buttonRemoveAuto;

	private JComboBox comboBoxTypeAuto;

	private JLabel label_1,label_2,label_3,addConfigAutos,typeAuto1,typeAuto2,typeAuto3;

	private JSpinner spinnerType1,spinnerType2,spinnerType3;

	private JToggleButton tglbtnSingolaAuto;

	private JFormattedTextField formattedTextFieldSource,formattedTextFieldTarget;

	private Icon addAutoImg,clearImg,playImg,stopImg,pauseImg,removeCar;

	private JSeparator separator,separator_2;

	private JList listTmpAutoToAdd;

	//lista
	private ArrayList<Auto> listTmpAuto;
	
	private DefaultListModel modelList;

	private JScrollPane scrollPane;


	public GeneralPanel(Broker broker) {


		this.modelList = new DefaultListModel();

		this.listTmpAuto = new ArrayList<Auto>();

		this.addAutoImg = new ImageIcon("img/addCar.jpg");
		this.clearImg = new ImageIcon("img/clear2.png");
		this.playImg = new ImageIcon("img/play.png");
		this.stopImg = new ImageIcon("img/stop.png");
		this.pauseImg = new ImageIcon("img/pause.png");
		this.removeCar = new ImageIcon("img/car-delete.png");


		//registrazione!!!
		broker.addObserver(this);

		this.broker = broker;
		//this.inAction = false;

		chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

		this.setPreferredSize(new Dimension(250, 600));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{93, 104, 117};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 35, 62, 36, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		startButton = new JButton(playImg);
		startButton.setFont(new Font("Stencil", Font.PLAIN, 13));
		GridBagConstraints gbc_startButton = new GridBagConstraints();
		gbc_startButton.insets = new Insets(0, 0, 5, 5);
		gbc_startButton.gridx = 0;
		gbc_startButton.gridy = 0;
		startButton.addActionListener(this);
		add(startButton, gbc_startButton);

		stopButton = new JButton(stopImg);
		stopButton.addActionListener(this);
		stopButton.setFont(new Font("Stencil", Font.PLAIN, 13));
		GridBagConstraints gbc_stopButton = new GridBagConstraints();
		gbc_stopButton.insets = new Insets(0, 0, 5, 0);
		gbc_stopButton.gridx = 2;
		gbc_stopButton.gridy = 0;
		stopButton.addActionListener(this);

		btnPause = new JButton(pauseImg);
		btnPause.addActionListener(this);
		btnPause.setFont(new Font("Tahoma", Font.BOLD, 13));
		GridBagConstraints gbc_btnPause_1 = new GridBagConstraints();
		gbc_btnPause_1.insets = new Insets(0, 0, 5, 5);
		gbc_btnPause_1.gridx = 1;
		gbc_btnPause_1.gridy = 0;
		add(btnPause, gbc_btnPause_1);
		add(stopButton, gbc_stopButton);

		impostaMappaButton = new JButton("Seleziona mappa");
		impostaMappaButton.setFont(new Font("Stencil", Font.PLAIN, 13));
		GridBagConstraints gbc_btnImpostaMappa = new GridBagConstraints();
		gbc_btnImpostaMappa.insets = new Insets(0, 0, 5, 0);
		gbc_btnImpostaMappa.gridwidth = 3;
		gbc_btnImpostaMappa.gridx = 0;
		gbc_btnImpostaMappa.gridy = 1;
		impostaMappaButton.addActionListener(this);
		add(impostaMappaButton, gbc_btnImpostaMappa);

		separator = new JSeparator();
		GridBagConstraints gbc_separator = new GridBagConstraints();
		gbc_separator.fill = GridBagConstraints.HORIZONTAL;
		gbc_separator.gridwidth = 3;
		gbc_separator.insets = new Insets(0, 0, 5, 0);
		gbc_separator.gridx = 0;
		gbc_separator.gridy = 2;
		add(separator, gbc_separator);

		tglbtnSingolaAuto = new JToggleButton("SINGOLA AUTO");
		GridBagConstraints gbc_tglbtnSingolaAuto = new GridBagConstraints();
		gbc_tglbtnSingolaAuto.gridwidth = 3;
		gbc_tglbtnSingolaAuto.insets = new Insets(0, 0, 5, 0);
		gbc_tglbtnSingolaAuto.gridx = 0;
		gbc_tglbtnSingolaAuto.gridy = 3;

		tglbtnSingolaAuto.addActionListener(this);
		add(tglbtnSingolaAuto, gbc_tglbtnSingolaAuto);

		label_1 = new JLabel("Tipologia");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 0;
		gbc_label_1.gridy = 4;
		add(label_1, gbc_label_1);

		comboBoxTypeAuto = new JComboBox(new String[]{"Utilitaria","Sportiva","Suv"});
		comboBoxTypeAuto.setSelectedIndex(-1);


		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 4;
		add(comboBoxTypeAuto, gbc_comboBox);

		label_2 = new JLabel("Partenza");
		GridBagConstraints gbc_label_2 = new GridBagConstraints();
		gbc_label_2.insets = new Insets(0, 0, 5, 5);
		gbc_label_2.gridx = 0;
		gbc_label_2.gridy = 5;
		add(label_2, gbc_label_2);

		formattedTextFieldSource = new JFormattedTextField();
		formattedTextFieldSource.setEditable(false);
		GridBagConstraints gbc_formattedTextFieldSource = new GridBagConstraints();
		gbc_formattedTextFieldSource.insets = new Insets(0, 0, 5, 5);
		gbc_formattedTextFieldSource.fill = GridBagConstraints.HORIZONTAL;
		gbc_formattedTextFieldSource.gridx = 1;
		gbc_formattedTextFieldSource.gridy = 5;
		add(formattedTextFieldSource, gbc_formattedTextFieldSource);

		btnClearSelectionSigleAuto = new JButton(clearImg);
		btnClearSelectionSigleAuto.addActionListener(this);
		GridBagConstraints gbc_btnClearSource = new GridBagConstraints();
		gbc_btnClearSource.gridheight = 3;
		gbc_btnClearSource.insets = new Insets(0, 0, 5, 0);
		gbc_btnClearSource.gridx = 2;
		gbc_btnClearSource.gridy = 4;
		add(btnClearSelectionSigleAuto, gbc_btnClearSource);

		label_3 = new JLabel("Arrivo");
		GridBagConstraints gbc_label_3 = new GridBagConstraints();
		gbc_label_3.insets = new Insets(0, 0, 5, 5);
		gbc_label_3.gridx = 0;
		gbc_label_3.gridy = 6;
		add(label_3, gbc_label_3);

		//GridBagConstraints gbc_btnPause_1 = new GridBagConstraints();


		gbc_btnPause_1.insets = new Insets(0, 0, 5, 0);
		gbc_btnPause_1.gridwidth = 5;
		gbc_btnPause_1.gridx = 0;
		gbc_btnPause_1.gridy = 10;

		formattedTextFieldTarget = new JFormattedTextField();
		formattedTextFieldTarget.setEditable(false);
		GridBagConstraints gbc_formattedTextFieldTarget = new GridBagConstraints();
		gbc_formattedTextFieldTarget.insets = new Insets(0, 0, 5, 5);
		gbc_formattedTextFieldTarget.fill = GridBagConstraints.HORIZONTAL;
		gbc_formattedTextFieldTarget.gridx = 1;
		gbc_formattedTextFieldTarget.gridy = 6;
		add(formattedTextFieldTarget, gbc_formattedTextFieldTarget);

		buttonAddAuto = new JButton(addAutoImg);
		GridBagConstraints gbc_buttonAddAuto = new GridBagConstraints();
		gbc_buttonAddAuto.insets = new Insets(0, 0, 5, 5);
		gbc_buttonAddAuto.gridx = 0;
		gbc_buttonAddAuto.gridy = 7;
		buttonAddAuto.addActionListener(this);

		add(buttonAddAuto, gbc_buttonAddAuto);

		buttonRemoveAuto = new JButton(removeCar);
		buttonRemoveAuto.addActionListener(this);
		GridBagConstraints gbc_buttonRemoveAuto = new GridBagConstraints();
		gbc_buttonRemoveAuto.insets = new Insets(0, 0, 5, 0);
		gbc_buttonRemoveAuto.gridx = 2;
		gbc_buttonRemoveAuto.gridy = 7;
		add(buttonRemoveAuto, gbc_buttonRemoveAuto);

		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 8;
		add(scrollPane, gbc_scrollPane);


		listTmpAutoToAdd = new JList();



		scrollPane.setViewportView(listTmpAutoToAdd);

		listTmpAutoToAdd.setModel(modelList);

		listTmpAutoToAdd.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		separator_2 = new JSeparator();
		GridBagConstraints gbc_separator_2 = new GridBagConstraints();
		gbc_separator_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_separator_2.gridwidth = 3;
		gbc_separator_2.insets = new Insets(0, 0, 5, 0);
		gbc_separator_2.gridx = 0;
		gbc_separator_2.gridy = 9;
		add(separator_2, gbc_separator_2);

		addConfigAutos = new JLabel("Aggiungi configurazione auto");
		addConfigAutos.setFont(new Font("Tahoma", Font.BOLD, 15));
		GridBagConstraints gbc_label_4 = new GridBagConstraints();
		gbc_label_4.insets = new Insets(0, 0, 5, 0);
		gbc_label_4.gridwidth = 3;
		gbc_label_4.gridx = 0;
		gbc_label_4.gridy = 10;
		add(addConfigAutos, gbc_label_4);

		typeAuto1 = new JLabel("Utilitaria");
		GridBagConstraints gbc_label_5 = new GridBagConstraints();
		gbc_label_5.insets = new Insets(0, 0, 5, 5);
		gbc_label_5.gridx = 0;
		gbc_label_5.gridy = 11;
		add(typeAuto1, gbc_label_5);

		spinnerType1 = new JSpinner();
		spinnerType1.setModel(new SpinnerNumberModel(0, 0, 20, 1));
		GridBagConstraints gbc_spinner = new GridBagConstraints();
		gbc_spinner.insets = new Insets(0, 0, 5, 5);
		gbc_spinner.gridx = 1;
		gbc_spinner.gridy = 11;
		add(spinnerType1, gbc_spinner);

		typeAuto2 = new JLabel("Sportiva");
		GridBagConstraints gbc_label_6 = new GridBagConstraints();
		gbc_label_6.insets = new Insets(0, 0, 5, 5);
		gbc_label_6.gridx = 0;
		gbc_label_6.gridy = 12;
		add(typeAuto2, gbc_label_6);

		spinnerType2 = new JSpinner();
		spinnerType2.setModel(new SpinnerNumberModel(0, 0, 20, 1));
		GridBagConstraints gbc_spinner_1 = new GridBagConstraints();
		gbc_spinner_1.insets = new Insets(0, 0, 5, 5);
		gbc_spinner_1.gridx = 1;
		gbc_spinner_1.gridy = 12;
		add(spinnerType2, gbc_spinner_1);

		typeAuto3 = new JLabel("Suv");
		GridBagConstraints gbc_label_7 = new GridBagConstraints();
		gbc_label_7.insets = new Insets(0, 0, 5, 5);
		gbc_label_7.gridx = 0;
		gbc_label_7.gridy = 13;
		add(typeAuto3, gbc_label_7);

		spinnerType3 = new JSpinner();
		spinnerType3.setModel(new SpinnerNumberModel(0, 0, 20, 1));
		GridBagConstraints gbc_spinner_2 = new GridBagConstraints();
		gbc_spinner_2.insets = new Insets(0, 0, 5, 5);
		gbc_spinner_2.gridx = 1;
		gbc_spinner_2.gridy = 13;
		add(spinnerType3, gbc_spinner_2);

		buttonAddConfAuto = new JButton("Aggiungi configurazione auto");
		buttonAddConfAuto.addActionListener(this);
		GridBagConstraints gbc_button_1 = new GridBagConstraints();
		gbc_button_1.gridwidth = 3;
		gbc_button_1.gridx = 0;
		gbc_button_1.gridy = 14;
		add(buttonAddConfAuto, gbc_button_1);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub


		if(e.getSource() == impostaMappaButton)
		{
			chooser.showOpenDialog(this);
			String pathMap = "mappe\\"+chooser.getSelectedFile().getName();
			broker.initParserAndNavigatore(pathMap);
			broker.notifyObservers("loadMap");


		}
		else if(e.getSource() == startButton){

			if(broker.getMatPath() == null)
				JOptionPane.showMessageDialog(null, "Caricare una mappa per iniziare la simulazione!", "Attenzione!", JOptionPane.WARNING_MESSAGE);
			else
			{
				if(broker.isSuspended()){
					broker.setSuspended(false);
					broker.getNavigatore().doActivate();
				}


				if(!listTmpAuto.isEmpty()){

					broker.setAutoToAdd(listTmpAuto);
					broker.notifyObservers("pippo");
					modelList.clear();
					listTmpAuto.clear();
				}
			}

		}

		else if (e.getSource() == btnPause){


			if(!broker.isSuspended() && broker.getNavigatore() != null){

				broker.getNavigatore().doSuspend();
				broker.setSuspended(true);
			}

		}

		else if(e.getSource() == stopButton){

			//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PROBELMA
			//quando si stoppa non ripartono i bheaviour! 

//			if(broker.getNavigatore() != null)
//			{
//			
//				//dorebbe ripulire tutte le strutture dati del navigatore lasciandolo in vita!
//				broker.getNavigatore().doSuspend();
//				
//				{
//					broker.getNavigatore().getIncroci().clear();
//					broker.getNavigatore().getWaitingCarsWithoutWays().clear();
//					broker.getNavigatore().getWaitingCarsWithWays().clear();
//					broker.getNavigatore().getAutos().clear();
//					
//				}
//				
//				broker.getNavigatore().doActivate();
//				broker.notifyObservers("repaintMapPanel");
//				
//			}

			
			broker.deleteNavigatoreFromJADE();
			
			broker.resetBroker();
			
			broker.notifyObservers("repaintMapPanel");
			
		}

		else if(e.getSource() == tglbtnSingolaAuto){

			if(tglbtnSingolaAuto.isSelected()){
				broker.setAutoAdd(true);
				broker.notifyObservers("activeAddAuto");

			}
			else{
				broker.setAutoAdd(false);
				broker.notifyObservers("disactiveAddAuto");

			}

		}

		else if(e.getSource() == buttonAddAuto){

			if(comboBoxTypeAuto.getSelectedIndex() == -1 || formattedTextFieldSource.getText().equals("") || formattedTextFieldTarget.getText().equals("")){

				if(comboBoxTypeAuto.getSelectedIndex() == -1)
					JOptionPane.showMessageDialog(null, "Devi selezionare una tipologia prima di aggiungere un auto!", "Attenzione!", JOptionPane.WARNING_MESSAGE);

				else if(formattedTextFieldSource.getText().equals("") || formattedTextFieldTarget.getText().equals(""))
					JOptionPane.showMessageDialog(null, "Devi indicare partenza e destinazione prima di aggiungere un auto!", "Attenzione!", JOptionPane.WARNING_MESSAGE);
			}
			else{

				Auto tmpAuto = new Auto();

				tmpAuto.setStart(new Triple<Integer, Integer, String>((int)broker.getSource().getWidth(),(int)broker.getSource().getHeight(),"INTERSECTION"));
				tmpAuto.setEnd(new Triple<Integer, Integer, String>((int)broker.getTarget().getWidth(),(int)broker.getTarget().getHeight(),"INTERSECTION"));
				tmpAuto.setKm(0);
				tmpAuto.setSpeed(1);
				tmpAuto.setMaxSpeed(3);
				tmpAuto.setAutoPosition(broker.getSource());
				tmpAuto.setId(broker.getContatoreIdAuto());
				broker.incrementIdAuto();

				listTmpAuto.add(modelList.getSize(),tmpAuto);

				modelList.addElement("Id Auto"+tmpAuto.getId());


				this.comboBoxTypeAuto.setSelectedIndex(-1);
				this.formattedTextFieldSource.setText("");
				this.formattedTextFieldTarget.setText("");
				//this.tglbtnSingolaAuto.setSelected(false);

			}

		}

		else if(e.getSource() == buttonRemoveAuto){

			int index = listTmpAutoToAdd.getSelectedIndex();

			if(index != -1){
				modelList.remove(index);

				listTmpAuto.remove(index);
			}
			else
				JOptionPane.showMessageDialog(null, "Devi selezionare una auto per eliminarla!", "Attenzione!", JOptionPane.WARNING_MESSAGE);

		}

		else if(e.getSource() == btnClearSelectionSigleAuto){

			this.formattedTextFieldSource.setText("");
			this.formattedTextFieldTarget.setText("");
			this.comboBoxTypeAuto.setSelectedIndex(-1);
			this.modelList.clear();

			broker.setLastChoice(false);
		}


		else if(e.getSource() == buttonAddConfAuto){

			if((Integer)spinnerType1.getModel().getValue() == 0 && 
					(Integer)spinnerType2.getModel().getValue() == 0 && 
					(Integer)spinnerType3.getModel().getValue() == 0){

				JOptionPane.showMessageDialog(null, "Devi indicare il numero di auto da caricare nella mappa!", "Attenzione!", JOptionPane.WARNING_MESSAGE);
			}
			else{

				//devo fare partire, specificando in modo random partenza e destinazione, 
				//il numero di car specificato negli Spinner!

			}

		}

	}

	@Override
	public void update(Object object) {
		// TODO Auto-generated method stub

		if(object.equals("setSourceAuto")){
			formattedTextFieldSource.setText(broker.getSource().getWidth()+"-"+broker.getSource().getHeight());
		}
		else if(object.equals("setTargetAuto")){
			formattedTextFieldTarget.setText(broker.getTarget().getWidth()+"-"+broker.getTarget().getHeight());

		}
	}	

}
